package com.eduspace.eduplatform.wares.utils;

import com.eduspace.eduplatform.wares.exception.ParamErrorException;
import lombok.extern.slf4j.Slf4j;
import pub.tbc.toolkit.Objs;
import pub.tbc.toolkit.function.Function;

import java.util.regex.Pattern;


/**
 * @author tbc
 * @version 1.0 {2016年8月1日 下午3:58:49}
 */
@Slf4j
public class TypeConverter {

    public static Double toDouble(String source) {
        return typeConvert(source, new Function<String, Double>() {
            @Override
            public Double apply(String input) {
                return Double.valueOf(input);
            }
        }, Double.class);
    }

    public static Long toLong(String source) {
        return typeConvert(source, new Function<String, Long>() {
            @Override
            public Long apply(String input) {
                return Long.valueOf(input);
            }
        }, Long.class);
    }

    public static Integer toInteger(String source) {
        return typeConvert(source, new Function<String, Integer>() {
            @Override
            public Integer apply(String input) {
                return Integer.valueOf(input);
            }
        }, Integer.class);
    }

    public static Float toFloat(String source) {
        return typeConvert(source, new Function<String, Float>() {
            @Override
            public Float apply(String s) {
                return Float.parseFloat(s);
            }
        }, Float.class);
    }

    public static <T> T typeConvert(String source, Function<String, T> fun) {
        if (isNumber(source)) {
            return null;
        }
        try {
            return fun.apply(source);
        } catch (Exception e) {
            String msg = "参数错误，参数:" + source + ", 转换失败 ";
            log.error(msg + "{}", e.getMessage());
            throw new ParamErrorException(msg);
        }
    }

    public static <T, E> T typeConvert(String source, Function<String, T> fun, Class<T> c) {
        if (isNumber(source)) {
            return null;
        }
        try {
            return fun.apply(source);
        } catch (Exception e) {
            String msg = "参数错误，参数:" + source + ",不能转换为类型 " + c.getName();
            log.error(msg + " -> exception-msg: {}", e.getMessage());
            throw new ParamErrorException(msg);
        }
    }

    /**
     * 判断字符串是否可转为数值类型
     *
     * @param source 原始字符串
     * @return 是否能转为数值类型
     */
    public static boolean isNumber(String source) {
        return Objs.nonEmpty(source) && Pattern.compile(MyConstantStore.IS_NUMBER).matcher(source).matches();
    }

//    public static void main(String[] args) {
//        System.out.println(toLong("0"));
//        System.out.println(toDouble("0.0"));
//        int a = "adbc".length();
//        System.out.println(a);
//
//        Integer.valueOf(1);
//
//        char aa = 'a';
//        char[] as = {'a', 'b'};
//        String ss = new String(as);
//        String.valueOf(as);
//        short bb = (short) aa;
//        int cc = bb;
//        long dd = cc;
//        float ee = dd;
//        double ff = ee;
//        System.out.println(ff);
//
//
//        System.out.println(toInteger("0.0"));
//
//    }

}
